package kz.kstu.scantest.app.delegate.impl;

import kz.kstu.scantest.app.delegate.api.FormDataDelegate;
import kz.kstu.scantest.app.util.locator.ServiceLocator;
import kz.kstu.scantest.app.util.locator.factory.ServiceLocatorFactory;
import kz.kstu.scantest.business.common.form.FormType;
import kz.kstu.scantest.business.entity.data.FormData;
import kz.kstu.scantest.business.entity.guide.School;
import kz.kstu.scantest.facade.ejb.api.FormDataFacade;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * @author Filipp_Stankevich
 *         created: 14.09.11 22:30
 */
public class FormDataDelegateImpl implements FormDataDelegate {

    public List<FormData> checkFormData(List<FormData> formDataList) {
        return getFormDataFacade(true).checkFormData(formDataList);
    }

    @Override
    public FormData processFormData(byte[] data, School school, FormType formType) throws UnsupportedEncodingException {
        return getFormDataFacade(false).processFormData(data, school, formType);
    }

    @Override
    public FormData validateAndSaveFormData(FormData formData) {
        return getFormDataFacade(false).validateAndSaveFormData(formData);
    }

    @Override
    public List<FormData> findByDateAndSchool(Date date, School school) {
        return getFormDataFacade(false).findByDateAndSchool(date, school);
    }

    @Override
    public List<FormData> findByDateAndSchoolWithErrors(Date date, School school) {
        return getFormDataFacade(false).findByDateAndSchoolWithErrors(date, school);
    }

    @Override
    public void saveFormData(FormData formData) {
        getFormDataFacade(false).saveFormData(formData);
    }

    @Override
    public void removeFormData(Long id) {
        getFormDataFacade(false).removeFormData(id);
    }

    @Override
    public List<Date> findScanDates() {
        return getFormDataFacade(false).findScanDates();
    }

    @Override
    public Integer getProcessedFormDataCount() {
        return getFormDataFacade(true).getProcessedFormDataCount();
    }

    @Override
    public void changeStatistic(Integer count) {
        getFormDataFacade(true).changeStatistic(count);
    }

    @Override
    public FormData findFormDataById(Long id) {
        return getFormDataFacade(false).findFormDataById(id);
    }

    private FormDataFacade getFormDataFacade(boolean isOnline) {
        ServiceLocator serviceLocator = ServiceLocatorFactory.getServiceLocator(isOnline);
        return serviceLocator.getService(FormDataFacade.SERVICE_NAME, FormDataFacade.class);
    }
}
